package com.franklin.java.utils;

import com.flychord.jdk.util.tuple.Tuple2;
import com.flychord.jdk.util.tuple.TupleAble;
import com.franklin.java.api.ui.jpanel.MyJScrollPane;
import com.franklin.java.icon.PluginIcons;
import com.franklin.plugins.common.utils.DataCheckUtils;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.ui.InputValidator;
import com.intellij.openapi.ui.MessageDialogBuilder;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.NlsSafe;
import com.intellij.ui.components.JBLabel;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.util.Objects;

import static com.flychord.jdk.util.tuple.TupleAble.tuple;

/**
 * swing 工具类
 *
 * @author Franklin
 * @since 2021/2/5 10:47
 */
public class SwingUtils {

    public static Tuple2<Integer, Integer> getPositionInJComponent(MouseEvent e) {
        return getPositionInJComponent(e,null);
    }

    /**
     * 获取鼠标在组件内的坐标<br>
     *
     * @param e:
     * @return com.flychord.jdk.util.tuple.Tuple2<java.lang.Integer,java.lang.Integer>
     * @author Franklin
     * @since 2021/2/5 10:48
     */
    public static Tuple2<Integer, Integer> getPositionInJComponent(MouseEvent e, MyJScrollPane scrollPane) {
        Point point = MouseInfo.getPointerInfo().getLocation();
        int a = point.x;
        int b = point.y;
        int x = e.getXOnScreen();
        int y = e.getYOnScreen();
        int xc = e.getX();
        int yc = e.getY();
        int mouseInComponentX = (a - x) + xc;
        int mouseInComponentY = (b - y) + yc;

        //滚动条
        if (Objects.nonNull(scrollPane)){
            int scrollX = scrollPane.getHorizontalScrollSize();
            int scrollY = scrollPane.getVerticalScrollSize();
            mouseInComponentX -= scrollX;
            mouseInComponentY += scrollY;
        }

        return TupleAble.tuple(mouseInComponentX, mouseInComponentY);
    }

    public static GridLayoutManager createLayoutManager(int rowCount,int columnCount){
        GridLayoutManager gridLayoutManager = new GridLayoutManager(rowCount, columnCount);
        gridLayoutManager.setVGap(1);
        gridLayoutManager.setHGap(1);
        return gridLayoutManager;
    }

    /**
     * 输入弹窗<br>
     * @author Franklin
     * @since 2021/2/9 16:51
     * @param component:
     * @param message:
     * @param title:
     * @return java.lang.String
     */
    public static String showInputDialog(Component component,String message,String title){
        return  (String) JOptionPane.showInputDialog(
            component,
            message,
            title,
            JOptionPane.INFORMATION_MESSAGE,
            PluginIcons.SERVICE,
            null,
            null
        );
    }

    /**
     * 确认弹窗<br>
     * @author Franklin
     * @since 2021/2/9 17:03
     * @param component:
     * @param message:
     * @param title:
     * @return boolean
     */
    public static boolean showConfirmDialog(Component component,String message,String title){
        int result = JOptionPane.showConfirmDialog(component, message, title, JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, PluginIcons.SERVICE);
        return result == JOptionPane.OK_OPTION;
    }

    public static boolean showConfirmDialog(String title,String message){
        return MessageDialogBuilder.yesNo(title,message)
                .icon(PluginIcons.SERVICE)
                .isYes();
    }

    public static String showInputDialog(
        String message,
        String title,
        String initialValue
    ){
        return Messages.showInputDialog(message, title, PluginIcons.SERVICE, initialValue, new InputValidator() {
            @Override
            public boolean checkInput(@NlsSafe String inputString) {
                return DataCheckUtils.isNotEmpty(inputString);
            }

            @Override
            public boolean canClose(@NlsSafe String inputString) {
                return DataCheckUtils.isNotEmpty(inputString);
            }
        });
    }

    public static String showInputDialog(
        String message,
        String title,
        String initialValue,
        InputValidator inputValidator
    ){
        return Messages.showInputDialog(message,title,PluginIcons.SERVICE,initialValue,inputValidator);
    }

    public static void showTestMessage(JPanel showMessagePanel,boolean success,String message){
        showMessagePanel.removeAll();
        Icon icon = success ? AllIcons.RunConfigurations.TestPassed : AllIcons.RunConfigurations.TestError;
        JBLabel jbLabel = new JBLabel(message, icon, SwingConstants.LEFT);
        Dimension size = new Dimension(50, 30);
        showMessagePanel.add(
            jbLabel,
            new GridConstraints(0, 0, 1, 1,
                GridConstraints.ANCHOR_WEST, GridConstraints.FILL_BOTH,
                GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED,
                size, size, null)
        );
        showMessagePanel.repaint();
    }
}
